<?php



class SiteController extends BaseController
{

    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex()
    {
        $now = date('H:i:s d-m-Y');
        $start = date('18:30:00 d-m-Y');
        $end = date('20:00:00 d-m-Y');
        if($now >= $start && $now <= $end)
        {
            $this->render('../game/khoagame');
        }
        else
        {
            $this->render('index');
        }
    }

 

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error)
        {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }
    
    //Lien he
    public function actionContact()
    {
    	// using the default layout 'protected/views/layouts/main.php'
    	$this->render('lienhe');
    }
    
    //hoi dap
    public function actionHoidap()
    {
    	// using the default layout 'protected/views/layouts/main.php'
    	$data = array();
    	$setting = Yii::app()->db->createCommand()
					    	->select('*')
					    	->from('ld_news')
					    	->where('id=:v', array(':v'=>'2'))
					    	->queryRow();
    	$data['rule'] = $setting['content'];
    	$this->render('hoidap', $data);
    }
    //huongdan
    public function actionHuongdan()
    {
    	// using the default layout 'protected/views/layouts/main.php'
    	$data = array();
    	$setting = Yii::app()->db->createCommand()
						    	->select('*')
						    	->from('ld_news')
						    	->where('id=:v', array(':v'=>'1'))
						    	->queryRow();
    	$data['rule'] = $setting['content'];
    	$this->render('huongdan', $data);
    }
    //luat choi
    public function actionLuatchoi()
    {
    	// using the default layout 'protected/views/layouts/main.php'
    	$data = array();
    	$setting = Yii::app()->db->createCommand()
    							->select('*')
    							->from('ld_news')
    							->where('id=:v', array(':v'=>'3'))
    							->queryRow();
    	$data['rule'] = $setting['content'];
    	$this->render('luatchoi', $data);
    }

    public function actionRegister()
    {
        $model = new RegisterForm();
        if (isset($_POST['RegisterForm']))
        {
            $model->attributes = $_POST['RegisterForm'];
            $user_Model = new User;
            $user_Model->your_name = $_POST['RegisterForm']['your_name'];
            $user_Model->user_name = $_POST['RegisterForm']['user_name'];
            $user_Model->password = md5($_POST['RegisterForm']['password']);
            $user_Model->email = $_POST['RegisterForm']['email'];
            $user_Model->lr_acc = $_POST['RegisterForm']['lr_acc'];
            $user_Model->date_reg = date('Y-m-d H:i:s');
            
            if ($model->validate() && $user_Model->save())
            {
                 // send welcome mail
                $mailUtil = new MailUtil();
                //$mailUtil->sendWelcomeMail($user_Model);
                $mailUtil->sendActiveMail($user_Model);
                // Sau khi đăng kí thành công thì login luôn
//                $model = new LoginForm();
//                $model->username = $_POST['RegisterForm']['user_name'];
//                $model->password = $_POST['RegisterForm']['password'];
//                if ($model->validate() && $model->login())
//                {
//                    $this->redirect('index');
//                }
                // Sau khi đăng kí thành công thì hiện thông báo yêu cầu kích hoạt
                $this->render('register_success');
                return;
            }
        }
        $this->render('register', array('model' => $model));
    }

    public function actionLogin()
    {
        // Nếu đăng nhập rồi thì ko cần nữa
        if(!Yii::app()->user->isGuest)
        {
            $this->redirect(Yii::app()->homeUrl);
        }
        $model = new LoginForm();

        // collect user input data
        if (isset($_POST['LoginForm']))
        {
            $model->attributes = $_POST['LoginForm'];
   
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
            {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout()
    {
        Yii::app()->user->logout();
        // clear session
        Yii::app()->session->destroy();
        $this->redirect(Yii::app()->homeUrl);
    }

    public function actionLostPassword()
    {
        $model = new LostPassForm();
        if(isset ($_POST['LostPassForm']))
        {
            $model->setAttributes($_POST['LostPassForm']);
            if($model->validate())
            {
                $user = User::model()->find('email = :email', array(':email'=>$model->email));
                if(!$user)
                {
                    $model->addError('email', 'Địa chỉ email này không tồn tại trong hệ thống của chúng tôi. Xin thử lại');
                }
                else
                {
                    $mailUtil = new MailUtil();
                    if($mailUtil->sendResetMail($user))
                    {
                        $this->render('lostpass_success');
                    }
                }
            }
        }
        $this->render('lost_password', array('model'=>$model));
    }
    
    public function actionLayDanhSachPhongThan()
    {
        $lstPhongThan = LoDe::layDanhSachPhongThan();
        $this->renderPartial('phong_than', array('lstPhongThan'=>$lstPhongThan));
    }
    
    public function actionNewDetail()
    {
        $thong_bao = Yii::app()->db->createCommand()
                                        ->select('*')
                                        ->from('ld_news')
                                        ->where('id=:v', array(':v' => '4'))
                                        ->queryRow();
        $this->layout = '//layouts/notice_layout';
        $this->render('thong_bao', array('new_content'=>$thong_bao['content']));
    }
    
    public function actionXemKetQuaXoSo()
    {
        $ketquaxs = Ketquaxoso::model()->findBySql("SELECT DATE_FORMAT(a.date,'%d/%m/%Y') as date, a.x80, a.x8, a.x3dot5 FROM ld_ketquaxoso a ORDER BY a.date DESC LIMIT 0,1");
        $this->render('ketqua_xoso', array('model' => $ketquaxs));
    }
    
    public function actionAjaxXemKetQuaXoSo()
    {
        $date = (isset($_GET['view_date']) && $_GET['view_date'] !== '') ? $_GET['view_date'] : date('Y-m-d');
        
        $ketquaxs = Ketquaxoso::model()->findBySql("SELECT DATE_FORMAT(a.date,'%d/%m/%Y') as date, a.x80, a.x8, a.x3dot5 FROM ld_ketquaxoso a  WHERE a.date = '$date' ORDER BY a.date DESC LIMIT 0,1");
        //var_dump($ketquaxs);
        if($ketquaxs)
        {
            $this->renderPartial('ketqua_xoso_ajax', array('model' => $ketquaxs));
        }
        else
        {
            echo '<strong>Không có kết quả xổ số cho ngày '.$date.'</strong>';
        }
    }
}